using ASP;
using HoWave.Common;
using HoWave.Data;
using HoWave.Web;
using HoWave.Web.Shop.Member;
using System;
using System.Data;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI.WebControls;
public class Shop_Manage_shop_friend_link : Member, IRequiresSessionState
{
	protected Label LabFriendLinkCount;
	protected GridView GVFriendnLink;
	protected CheckBox CheckAllFriendLink;
	protected Button ButDeleteFrientLink;
	protected TextBox TxtU_NickName;
	protected Button Button1;
	protected Label Label1;
	private int ShopID = -1;
	protected DefaultProfile Profile
	{
		get
		{
			return (DefaultProfile)this.Context.Profile;
		}
	}
	protected global_asax ApplicationInstance
	{
		get
		{
			return (global_asax)this.Context.ApplicationInstance;
		}
	}
	protected void Page_Load(object sender, EventArgs e)
	{
		this.ShopID = base.user.ShopID;
		if (this.ShopID <= 0)
		{
			this.WriteErrMsg("友情提示", Language.GetMemberMsg("Common/NoShop", new string[0]), "../Member/Shop_OpenShop.aspx");
		}
		if (!base.IsPostBack)
		{
			this.DataBandGVFriendLink();
		}
	}
	protected void ButAddFrientLike_Click(object sender, EventArgs e)
	{
		string text = Text.ReplaceBadChar(this.TxtU_NickName.Text.Trim());
		if (string.IsNullOrEmpty(text))
		{
			MessageBox.Show(this, "输入不能为空！");
		}
		else
		{
			if (text == base.user.UserName)
			{
				MessageBox.Show(this, "不能添加自己！");
			}
			else
			{
				UserBll userBll = new UserBll();
				DataTable dataTable = new DataTable();
				dataTable = userBll.GetUserByUserName(text);
				int count = dataTable.Rows.Count;
				if (count <= 0)
				{
					MessageBox.Show(this, "没有此用户");
				}
				else
				{
					int friendUserID = int.Parse(dataTable.Rows[0]["UserID"].ToString());
					if (dataTable.Rows[0]["ShopID"] == null || dataTable.Rows[0]["ShopID"].ToString() == "" || dataTable.Rows[0]["ShopID"].ToString() == "-1" || dataTable.Rows[0]["ShopID"].ToString() == "0")
					{
						MessageBox.Show(this, "此用户还没开店");
					}
					else
					{
						Shop_FriendLink shop_FriendLink = new Shop_FriendLink();
						if (shop_FriendLink.ExistFriendLink(this.ShopID, friendUserID))
						{
							MessageBox.Show(this, "此连接已经存在了");
						}
						else
						{
							if (shop_FriendLink.IsMaxLink(this.ShopID))
							{
								MessageBox.Show(this, "已达到最大的连接数量35");
							}
							else
							{
								shop_FriendLink.InsertFriendLink(this.ShopID, friendUserID);
								this.TxtU_NickName.Text = "";
								string userName = base.user.UserName;
								MessageBll messageBll = new MessageBll();
								messageBll.AddMessage(userName, "Users", "系统消息---新友情链接", string.Concat(new object[]
								{
									"你的店铺已经被<a href='../Shop1/myShop.aspx?ID='",
									this.ShopID,
									"' target='_blank'>",
									userName,
									"</a>加为友情链接;"
								}), 0, 0, 1, 0, text, "0", 1);
								this.DataBandGVFriendLink();
							}
						}
					}
				}
			}
		}
	}
	private void DataBandGVFriendLink()
	{
		Shop_FriendLink shop_FriendLink = new Shop_FriendLink();
		this.GVFriendnLink.DataSource = shop_FriendLink.GetFriendLinkByShopID(this.ShopID);
		this.GVFriendnLink.DataBind();
	}
	protected void GridViewLink_Updated(object sender, GridViewUpdatedEventArgs e)
	{
	}
	protected void GVFriendnLink_DataBound(object sender, EventArgs e)
	{
		this.LabFriendLinkCount.Text = this.GVFriendnLink.Rows.Count.ToString();
	}
	protected void CheckAllFriendLink_CheckedChanged(object sender, EventArgs e)
	{
		if (this.CheckAllFriendLink.Checked)
		{
			foreach (GridViewRow gridViewRow in this.GVFriendnLink.Rows)
			{
				CheckBox checkBox = (CheckBox)gridViewRow.Cells[0].FindControl("CheckFriendLink");
				checkBox.Checked = true;
			}
		}
		else
		{
			foreach (GridViewRow gridViewRow in this.GVFriendnLink.Rows)
			{
				CheckBox checkBox = (CheckBox)gridViewRow.Cells[0].FindControl("CheckFriendLink");
				checkBox.Checked = false;
			}
		}
	}
	protected void ButDeleteFrientLink_Click1(object sender, EventArgs e)
	{
		Shop_FriendLink shop_FriendLink = new Shop_FriendLink();
		int num = 0;
		foreach (GridViewRow gridViewRow in this.GVFriendnLink.Rows)
		{
			CheckBox checkBox = (CheckBox)gridViewRow.Cells[0].FindControl("CheckFriendLink");
			int friendUserID = int.Parse(checkBox.Text);
			if (checkBox.Checked)
			{
				shop_FriendLink.DeleteFriendLink(this.ShopID, friendUserID);
				num++;
			}
		}
		if (num > 0)
		{
			MessageBox.Show(this, "你已经成功地删除了" + num + "条记录");
			this.DataBandGVFriendLink();
		}
		else
		{
			MessageBox.Show(this, "请你先选好要删除的记录");
		}
	}
	protected void CheckAllFriendLink_CheckedChanged1(object sender, EventArgs e)
	{
		if (this.CheckAllFriendLink.Checked)
		{
			foreach (GridViewRow gridViewRow in this.GVFriendnLink.Rows)
			{
				CheckBox checkBox = (CheckBox)gridViewRow.Cells[0].FindControl("CheckFriendLink");
				checkBox.Checked = true;
			}
		}
		else
		{
			foreach (GridViewRow gridViewRow in this.GVFriendnLink.Rows)
			{
				CheckBox checkBox = (CheckBox)gridViewRow.Cells[0].FindControl("CheckFriendLink");
				checkBox.Checked = false;
			}
		}
	}
}
